package com.groupscore.score.service;

import com.groupscore.score.dao.PersonalScoreDao;
import com.groupscore.score.dao.ScoreDao;
import com.groupscore.score.pojo.PersonalScore;
import com.groupscore.score.pojo.Score;
import com.groupscore.score.pojogroup.OptionScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class PersonalScoreService {

    @Autowired
    private PersonalScoreDao personalScoreDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 打分
     */
    public void addPersonalScore(List<PersonalScore> personalscoreList, String userId){

        for(PersonalScore ps  : personalscoreList){

            PersonalScore personalScore = new PersonalScore();
            personalScore.setId(idWorker.nextId()+"");
            personalScore.setPersonalId("");
            personalScore.setPersonalName(ps.getPersonalName());
            personalScore.setOptionId(ps.getOptionId());
            personalScore.setScore(ps.getScore());
            personalScore.setUserId(userId);
            personalScore.setContent(ps.getContent());
            personalScore.setCreateTime(new Date());
            personalScoreDao.save(personalScore);
        }
    }

    /**
     * 修改分数
     */
    public void update(PersonalScore personalScore){
        personalScoreDao.save(personalScore);
    }

    /**
     * 删除分数
     */
    public void deleteById(String id){
        personalScoreDao.deleteById(id);
    }

    /**
     * 查询个人得分,及排名
     */
    public List<Map> personalScore(){
        return personalScoreDao.personalScore();
    }


    /**
     * 根据用户id查询给个人的打分
     * @param userId
     * @return
     */
    public List<Map> personalScoreByUserId(String userId){
        return personalScoreDao.groupScoreByUserId(userId);
    }

}
